﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Willow.Const;
using Willow.IService;
using Willow.IService.Auth;
using Willow.Model;
using Willow.MVC.Result;

namespace Willow.WebApplication.Areas.Auth.Controllers
{
    public class OrganizationController : Controller
    {

        #region 属性

        public IOrganizationService OrganizationService { get; set; }

        #endregion


        #region 页面

        /// <summary>
        /// 组织管理页
        /// </summary>
        /// <returns></returns>
        public ViewResult Management()
        {
            return View();
        }

        /// <summary>
        /// 组织机构页
        /// </summary>
        /// <returns></returns>
        public ViewResult OrgChart()
        {
            var org = OrganizationService.Filter(p => p.IsDelete == false);
            StringBuilder orgChartContent = new StringBuilder();
            ConvertToOrgChart(orgChartContent, org, DataBaseConst.DefaultParentId);
            ViewBag.OrgChartContent = orgChartContent.ToString();
            return View();
        }

        #endregion


        #region 操作

        /// <summary>
        /// 为树获取所有组织机构
        /// </summary>
        /// <returns></returns>
        public JsonResult GetAllOrganizationsForTree()
        {
            var data = OrganizationService
                .Filter(p => p.IsDelete == false)
                .OrderBy(p => p.DisplayOrder)
                .Select(p => new { id = p.Id, text = p.Name, p.ParentId })
                .ToList();
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 为树列表获取所有组织机构
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public JsonResult GetAllOrganizationsForTreeGrid(string name = "")
        {
            var data = OrganizationService
                .Query(p => p.IsDelete == false && p.Name.Contains(name))
                .OrderBy(p => p.DisplayOrder)
                .Select(p => new
                {
                    p.Id,
                    p.Name,
                    p.Remark,
                    p.ParentId,
                    p.FullParentId,
                    p.DisplayOrder,
                    p.IsDelete
                });
            return Json(data, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 保存组织机构
        /// </summary>
        /// <param name="organization">The organization.</param>
        /// <param name="action">The action.</param>
        /// <returns></returns>
        public JsonResult Save(Auth_Organization organization, SaveActionEnum action)
        {
            return this.StandardJson(() =>
            {
                OrganizationService.Save(organization, action);
            });
        }

        /// <summary>
        /// 删除组织机构
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public JsonResult Delete(Guid id)
        {
            return this.StandardJson(() =>
            {
                OrganizationService.LogicalDelete(id);
            });

        }

        /// <summary>
        /// 转换为组织机构HTML代码
        /// </summary>
        /// <param name="content">The content.</param>
        /// <param name="organizations">The organizations.</param>
        /// <param name="parentId">The parent identifier.</param>
        private void ConvertToOrgChart(StringBuilder content, IEnumerable<Auth_Organization> organizations, Guid parentId)
        {
            var listParent = organizations.Where(p => p.ParentId == parentId);
            if (listParent.Count() > 0)
            {
                content.Append("<ul " + (parentId == DataBaseConst.DefaultParentId ? @"id=""org"" style=""display:none""" : "") + ">");
                foreach (var item in listParent)
                {
                    content.Append(@"<li>");
                    content.Append(item.Name);
                    ConvertToOrgChart(content, organizations, item.Id);
                    content.Append(@"</li>");
                }
                content.Append(@"</ul>");
            }
        }

        #endregion

    }
}